package com.htfyun.eink;

import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.os.PowerManager;
import android.view.EinkPWInterface;
import android.view.View;

public class EinkUtils {
    private static final String PROP_FAST_SLEEP_TIME = "persist.pms.fastsleep.sec";

    /**
     * // 20210112: control bright-screen sleep.
     *
     * @param context
     * @param timeoutSec 0xff
     * @Deprecated
     */
    public static void setFastSleepTimeout(Context context, int timeoutSec) {
        PowerManager pm = context.getSystemService(PowerManager.class);
        if (pm == null) {
            return;
        }

        try {

            ReflectUtils.reflect(pm)
                    .method("setFastSleepTimeout", timeoutSec);

            System.out.println("setFastSleepTimeout ok");

        } catch (Exception e) {
            System.out.println("error setFastSleepTimeout + " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * // 20230415: control bright-screen sleep.
     *
     * @param timeoutSec 0 is disable
     */
    public static void setFastSleepTimeout(String timeoutSec) {
        SystemPropertyUtil.setSystemProperty(PROP_FAST_SLEEP_TIME, timeoutSec);
    }

    /**
     * // 20190223: the cpu freq level.
     *
     * @param context
     * @param cpu_level 0~12
     * @param lock
     */
    public static final int CPU_FREQ_LEVEL_MIN = 0;
    public static final int CPU_FREQ_LEVEL_MAX = 12;

    public static void setCpuFreq(Context context, int cpu_level, boolean lock) {
        PowerManager pm = context.getSystemService(PowerManager.class);
        if (pm == null) {
            return;
        }

        try {

            ReflectUtils.reflect(pm)
                    .method("setCpuFreq", cpu_level, lock);

            System.out.println("setCpuFreq ok");

        } catch (Exception e) {
            System.out.println("error setCpuFreq + " + e.getMessage());
            e.printStackTrace();
        }
    }

    public static void resetCpuFreqParam(Context context) {
        PowerManager pm = context.getSystemService(PowerManager.class);
        if (pm == null) {
            return;
        }

        try {

            ReflectUtils.reflect(pm)
                    .method("resetCpuFreqParam");
            System.out.println("resetCpuFreqParam ok");

        } catch (Exception e) {
            System.out.println("error resetCpuFreqParam + " + e.getMessage());

            e.printStackTrace();
        }
    }


    public static boolean isOTAUpgrade(Context context) {
        PackageManager pm = context.getSystemService(PackageManager.class);
        if (pm == null) {
            return false;
        }

        try {

            Object object = ReflectUtils.reflect(pm)
                    .method("isUpgrade").get();

            if (object instanceof Boolean) {
                return (Boolean) object;
            }

        } catch (Exception e) {
            System.out.println("error setCpuFreq + " + e.getMessage());
            e.printStackTrace();
        }

        return false;
    }


    /**
     * 获取EinkPWInterface的实例, 进行相关的手写操作.
     *
     * @param view 操作的View, 在其上面有个EinkPWInterface
     * @return
     */

    public static EinkPWInterface getEinkPWInterfaceWithView(View view) {
        try {
//            Method m = (Method) view.getClass().getMethod("getPWInterFace");
//            Object object = m.invoke(view);

            Object object = ReflectUtils.reflect(view)
                    .method("getPWInterFace").get();

            if (object instanceof EinkPWInterface) {
                return (EinkPWInterface) object;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 设置刷新模式
     *
     * @param view     需要设置刷新模式的View
     * @param dataMode 刷新的数据模式 {@link EinkDataMode}
     * @param showMode 刷新的显示模式 {@link EinkShowMode}
     */
    public static void setEinkUpdateModeWithView(View view, EinkDataMode dataMode, EinkShowMode showMode) {

        if (view == null) {
            return;
        }
        try {
//            Method method = (Method) view.getClass().getMethod("setEinkUpdateMode", Short.class, Short.class);
//            method.invoke(view, (short) dataMode.getDataMode(), (short) showMode.getShowMode());

            ReflectUtils.reflect(view)
                    .method("setEinkUpdateMode", (short) dataMode.getDataMode(), (short) showMode.getShowMode());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 设置刷新模式
     *
     * @param view     需要设置刷新模式的View
     * @param showMode 刷新的显示模式 {@link EinkShowMode}
     */
    public static void setEinkUpdateModeWithView(View view, EinkShowMode showMode) {

        if (view == null) {
            return;
        }
        try {

            ReflectUtils.reflect(view)
                    .method("setEinkUpdateMode", (short) showMode.getShowMode());

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 设置view内部区域的一个按下标志
     *
     * @param view
     */
    public static void enablePressedWithView(View view) {

        if (view == null) {
            return;
        }
        try {

            ReflectUtils.reflect(view)
                    .method("setPressedState");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 强制全屏刷新.
     *
     * @param view
     */
    public static void forceEinkFullUpdateWithView(View view) {

        if (view == null) {
            return;
        }
        try {

            ReflectUtils.reflect(view)
                    .method("forceEinkFullUpdate");

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 以指定的显示模式刷新View
     *
     * @param view
     * @param showMode 显示模式 {@link EinkShowMode}
     */
    public static void invalidateWithView(View view, EinkShowMode showMode) {

        if (view == null) {
            return;
        }
        try {

            ReflectUtils.reflect(view)
                    .method("invalidate", (short) showMode.getShowMode());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 以指定的显示模式刷新View
     *
     * @param view
     * @param dirty    刷新View的子区域
     * @param showMode 显示模式 {@link EinkShowMode}
     */
    public static void invalidateWithView(View view, Rect dirty, EinkShowMode showMode) {

        if (view == null) {
            return;
        }
        try {

            ReflectUtils.reflect(view)
                    .method("invalidate", dirty, (short) showMode.getShowMode());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 以指定的显示模式刷新View
     *
     * @param view
     * @param left
     * @param top
     * @param right
     * @param bottom
     * @param showMode {@link EinkShowMode}
     */
    public static void invalidateWithView(View view, int left, int top, int right, int bottom, EinkShowMode showMode) {

        if (view == null) {
            return;
        }
        try {

            ReflectUtils.reflect(view)
                    .method("invalidate", left, top, right, bottom, (short) showMode.getShowMode());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 息屏后延迟关闭WiFi时间，每单位500ms
     *
     * @param context
     * @param timeoutSec
     */
    public static void setWifiAutoOffTimeout(Context context, int timeoutSec) {
        PowerManager pm = context.getSystemService(PowerManager.class);
        if (pm == null) {
            return;
        }

        try {

            ReflectUtils.reflect(pm)
                    .method("setWifiAutoOffTimeout", timeoutSec);

            System.out.println("setWifiAutoOffTimeout ok");

        } catch (Exception e) {
            System.out.println("error setWifiAutoOffTimeout + " + e.getMessage());
            e.printStackTrace();
        }

    }

}
